package org.evil.deus.rss.widget;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class RSSWidgetHelper extends SQLiteOpenHelper {

	private static final String NAME = "widgets.db";
	private static final int VERSION = 1;
	
	public RSSWidgetHelper(Context context) {
		super(context, NAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		createWidgetTable(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		dropWidgetTable(db);
		createWidgetTable(db);
	}
	
	private void createWidgetTable(SQLiteDatabase db) {
		StringBuilder sb = new StringBuilder();
		sb.append("CREATE TABLE ").append(RSSWidget.Widget.TABLE).append("(");
		sb.append(RSSWidget.Widget._ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
		sb.append(RSSWidget.Widget.ID).append(" INTEGER,");
		sb.append(RSSWidget.Widget.URL).append(" TEXT);");
		db.execSQL(sb.toString());
	}
	
	private void dropWidgetTable(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + RSSWidget.Widget.TABLE);
	}
}
